from django.core.mail import send_mail
from apps.users.utils import generic_email_verify_token
from celery_tasks.celery_settings import app
import logging
@app.task
def celery_send_email(userid, email):
    # 发送邮件
    # subject message from_email recipient_list
    # 主题
    # 获取token
    token = generic_email_verify_token(userid)
    subject = '美多商城激活邮件验证'
    # 生成链接
    verify_url = 'http://www.meiduo.site:8080/success_verify_email.html?token=%s'%token
    # 消息 ---- 返回html文件
    html_message = f'<p>尊敬的用户您好！</p>' \
                   f'<p>感谢您使用美多商城<\p>' \
                   f'<p>您的邮箱为：{email}. 请点击此链接激活邮箱<\p>'\
                   f'<p><a href={verify_url}>{verify_url}</a></p>'

    # 发件人
    from_email = '18836096773@163.com'
    # 收件人列表
    recipient_list = [email]

    # 实例化一个logging对象
    logger = logging.getLogger('loggers')
    try:
        send_mail(subject=subject, html_message=html_message, from_email=from_email,
              recipient_list=recipient_list, message="")
    except Exception as e:
        logger.error(e)
